define(['module/ng-datatable/script'] , function(){
	var controller = function( $scope , $timeout , $ajax , $stateParams , $ctx ){
		console.log('参数列表' , $stateParams ) ;
		$scope.serviceOption = {
			option 	: 	{
				data 	: 	null ,
				key 	: 	'$id' , 
				text 	: 	'$serviceName' ,
				change 	: 	function( val , ele ){
					$scope.createServiceHandler.getService( val );
				}
			} ,
			data 	: 	''
		};

		//监控配置
		$scope.deviceServiceMeta =  {
			alertFields 	: 	[[{
				label 	: 	'严重' ,
				name 	: 	'crtExp' ,
				rule 	: 	{
					required 	: 	true ,
					maxlength 	: 	20 
				},
				type 	: 	'select' ,
				option 	: 	{
					data 	: 	$ctx.cfgApi + 'dict/THRESHOLD/type' ,
					key 	: 	'$value' ,
					text 	: 	'$dictName'
				},
				cols 	:  6
			},{
				label 	: 	'阈值' ,
				name 	: 	'critical' ,
				rule 	: 	{
					required 	: 	true ,
					maxlength 	: 	20 
				},
				cols 	:  6
			}],[{
				label 	: 	'警告' ,
				name 	: 	'warnExp' ,
				rule 	: 	{
					required 	: 	true ,
					maxlength 	: 	20 
				},
				type 	: 	'select' ,
				option 	: 	{
					data 	: 	$ctx.cfgApi + 'dict/THRESHOLD/type' ,
					key 	: 	'$value' ,
					text 	: 	'$dictName'
				},
				cols 	:  6
			},{
				label 	: 	'阈值' ,
				name 	: 	'warning' ,
				rule 	: 	{
					required 	: 	true ,
					maxlength 	: 	20 
				},
				cols 	:  6
			}]],
			paramFields 	:  [[]] ,
			data 	: 	{
				params 	: 	{}
			}
		};

		//实例列表
		$scope.deviceServiceInstanceTable = {
			setting : {
				aoColumns : 	[{
					mData 	: 	'id' ,
					sTitle 	: 	'ID' ,
					sWidth 	: 	'5%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	true ,
					render 	: 	function( data , st , row ){
						return data || '--' ;
					}
				},{
					mData 	: 	'instanceName' ,
					sTitle 	: 	'实例名称' ,
					sWidth 	: 	'8%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	false ,
					render 	: 	function( data , st , row ){
						return data || '--' ;
					}
				},{
					mData 	: 	'checkPeroid' ,
					sTitle 	: 	'检查频率' ,
					sWidth 	: 	'8%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	false ,
					render 	: 	function( data , st , row ){
						return data || '--' ;
					}
				},{
					mData 	: 	'critical' ,
					sTitle 	: 	'Critical' ,
					sWidth 	: 	'8%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	false ,
					render 	: 	function( data , st , row ){
						return data || '--' ;
					}
				},{
					mData 	: 	'warning' ,
					sTitle 	: 	'Warning' ,
					sWidth 	: 	'8%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	false ,
					render 	: 	function( data , st , row ){
						return data || '--' ;
					}
				},{
					mData 	: 	'note' ,
					sTitle 	: 	'描述' ,
					sWidth 	: 	'5%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	false ,
					render 	: 	function( data , st , row ){
						return data || '--' ;
					}
				},{
					mData 	: 	'createTime' ,
					sTitle 	: 	'创建日期' ,
					sWidth 	: 	'5%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	false ,
					render 	: 	function( data , st , row ){
						return data && new Date( data * 1000 ).format('yyyy/MM/dd') || '--' ;
					}
				},{
					mData 	: 	'createName' ,
					sTitle 	: 	'创建人' ,
					sWidth 	: 	'8%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	false ,
					render 	: 	function( data , st , row ){
						return data || '--' ;
					}
				},{
					mData 	: 	'id' ,
					sTitle 	: 	'操作' ,
					sWidth 	: 	'5%' ,
					sClass 	: 	'center nowrap' ,
					bSortable	: 	true ,
					render 	: 	function( data , st , row ){
						var btnGroup = $('<div></div>').addClass('btn-group-inline') ;
						$('<a>修改</a>').attr({
							'class' 	: 	'btn btn-info btn-xs' ,
							'data-id' 	: 	data ,
							'name' : 	'update-btn'
						}).appendTo( btnGroup ) ;

						$('<a>删除</a>').attr({
							'class' 	: 	'btn btn-danger btn-xs' ,
							'data-id' 	: 	data ,
							'name' : 	'delete-btn'
						}).appendTo( btnGroup ) ;

						return btnGroup.children().length && btnGroup[0].outerHTML || '暂无操作' ;
					}
				}],
				paging 	: 	false ,
				ajaxSource : []
			},
			table 	: 	undefined
		}
		//#实例列表

		$scope.createServiceHandler = {
			hasInstance : 0 ,
			data 	: 	{} ,
			init 	: 	function(){
				var _this = this ;
				this.initServiceForm() ;

				this.getDevice( $stateParams['deviceId'] , function(){
					$timeout( function(){
						$scope.serviceOption.option.data = $ctx.cfgApi + util.format('service?deviceGroupId=$deviceGroupId' ,  $stateParams ) ;
					});
				} );
				return this ;
			},
			initServiceForm 	: 	function(){
				var _this = this ;
				this.serviceForm = '#device-service-form' ;
				this.validator = $(this.serviceForm).validate( {} ) ;
				$('#cw').delegate(this.serviceForm + ' [name=positive-btn]' , 'click' , function(){
					_this.saveDeviceService() ;
				});
				return this ;
			},
			saveDeviceService : 	function(){
				if( $ctx.debug )
					console.log('保存监控配置:' , $scope.deviceServiceMeta.data ) ;
				if( !this.validator.form() )
					return false ;
				var param = {
					crtExp 	: 	$scope.deviceServiceMeta.data['crtExp'] ,
					critical:  	$scope.deviceServiceMeta.data['critical'] ,
					warnExp : 	$scope.deviceServiceMeta.data['warnExp'] ,
					warning : 	$scope.deviceServiceMeta.data['warning'] ,
					params 	: 	[]
				}
				if(  $scope.deviceServiceMeta.data.params ){
					for( var key in $scope.deviceServiceMeta.data.params ){
						param.params.push({
							varId 	: 	key ,
							varValue: 	$scope.deviceServiceMeta.data.params[key]
						});
					}
				}
				if( $ctx.debug )
					console.log('保存监控配置:' , param ) ;

				$ajax.post( $ctx.cfgApi + '/device/$deviceId/dim/$deviceDimId' , $stateParams ) , JSON.stringify( param )
					.success( function( result ){
						if( result.code != 0 )
							return alert( result.message ) ;
						alert( '保存成功!' ) ;
					});
			},
			getDevice 	: 	function( deviceId , then ){
				var _this = this ;
				$ajax.get($ctx.cfgApi + 'device/' + deviceId ).success( function( result ){
					if( result.code != 0 )
						return alert( result.message ) ;
					if( !result.data )
						return alert('设备不存在!') ;
					_this.device = result.data ;
					if( typeof then == 'function' )
						then( _this.device ) ;
				});
			},
			getService 	: 	function( serviceId , then ){
				if( !serviceId )
					return this.renderFrom( ) ;

				var _this = this ;
				$ajax.get( $ctx.cfgApi + util.format('service/' + serviceId ) )
					.success( function( result ){
						if( result.code != 0 )
							return alert( result.message ) ;
						if( !result.data ) 
							return alert( '数据不存在!' ) ;
						$timeout( function(){
							_this.hasInstance = result.data.hasInstance ;
						});
						if( !_this.hasInstance )
							_this.renderServiceForm( result.data['id']) ; 
						
						if( typeof then == 'function')
							then( result.data ) ;
					});
			},
			rendVarsForm 	: 	function( data ){
				var paramFields = [[]] ;
				if( data && data.length ){
					data.forEach( function( v , k ){
						paramFields[0].push({
							label 	: 	v['varLabel'] ,
							name 	: 	v['id'] ,
							rule 	: 	{
								required 	: 	true ,
								maxlength 	: 	20 
							},
							help 	: 	v['note'] ,
							cols 	:  6
						});
					});
				}
				
				$timeout( function(){
					$scope.deviceServiceMeta.paramFields = paramFields ;
				});
				return paramFields ;
			},
			removeInstance : function( index ){

			},
			loadVars 	: 	function( serviceId , deviceGroupId , then ){
				var _this = this ;
				$ajax.get( $ctx.cfgApi + util.format('service/$serviceId/$deviceGroupId/vars' , {
					serviceId 	: 	 serviceId ,
					deviceGroupId : 	deviceGroupId
				} ) ).success( function( result ){
				 		if( result.code != 0 )
				 			return alert( result.message ) ;
				 		if( result.data && result.data.length )
				 			_this.vars = result.data ;

				 		if( typeof then == 'function' )
				 			then( _this.vars ) ;
				 	});
			},
			renderFrom 	: 	function( data ){
				this.rendVarsForm() ;
			},
			renderServiceForm 	: 	function( serviceId ){
				var _this = this ;
				_this.loadVars( serviceId , _this.device['deviceGroup'] , function( vars ){
					_this.rendVarsForm( vars );
				}) ;
			}	
		}

		$timeout( function(){
			$scope.createServiceHandler.init() ;
		});
	}

	return controller ;
});